﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace QunYing.DAL
{
    /// <summary>
    /// 数据访问类:ServiceOrder
    /// </summary>
    public partial class ServiceOrder
    {
        public ServiceOrder()
        { }
        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(QunYing.Model.ServiceOrder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ServiceOrder(");
            strSql.Append("服务单号,经销商ID,会员卡号,设备编号,报警信息编号,出警方式,技工姓名,技工联系电话,出勤时间,处理方式,处理结果)");
            strSql.Append(" values (");
            strSql.Append("@服务单号,@经销商ID,@会员卡号,@设备编号,@报警信息编号,@出警方式,@技工姓名,@技工联系电话,@出勤时间,@处理方式,@处理结果)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@服务单号", SqlDbType.NVarChar,20),
					new SqlParameter("@经销商ID", SqlDbType.NVarChar,20),
					new SqlParameter("@会员卡号", SqlDbType.NVarChar,20),
					new SqlParameter("@设备编号", SqlDbType.NVarChar,20),
					new SqlParameter("@报警信息编号", SqlDbType.Int,4),
					new SqlParameter("@出警方式", SqlDbType.NVarChar,50),
					new SqlParameter("@技工姓名", SqlDbType.NVarChar,50),
					new SqlParameter("@技工联系电话", SqlDbType.NVarChar,50),
					new SqlParameter("@出勤时间", SqlDbType.DateTime),
					new SqlParameter("@处理方式", SqlDbType.NVarChar,50),
					new SqlParameter("@处理结果", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.服务单号;
            parameters[1].Value = model.经销商ID;
            parameters[2].Value = model.会员卡号;
            parameters[3].Value = model.设备编号;
            parameters[4].Value = model.报警信息编号;
            parameters[5].Value = model.出警方式;
            parameters[6].Value = model.技工姓名;
            parameters[7].Value = model.技工联系电话;
            parameters[8].Value = model.出勤时间;
            parameters[9].Value = model.处理方式;
            parameters[10].Value = model.处理结果;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(QunYing.Model.ServiceOrder model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ServiceOrder set ");
            strSql.Append("服务单号=@服务单号,");
            strSql.Append("经销商ID=@经销商ID,");
            strSql.Append("会员卡号=@会员卡号,");
            strSql.Append("设备编号=@设备编号,");
            strSql.Append("报警信息编号=@报警信息编号,");
            strSql.Append("出警方式=@出警方式,");
            strSql.Append("技工姓名=@技工姓名,");
            strSql.Append("技工联系电话=@技工联系电话,");
            strSql.Append("出勤时间=@出勤时间,");
            strSql.Append("处理方式=@处理方式,");
            strSql.Append("处理结果=@处理结果");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@服务单号", SqlDbType.NVarChar,20),
					new SqlParameter("@经销商ID", SqlDbType.NVarChar,20),
					new SqlParameter("@会员卡号", SqlDbType.NVarChar,20),
					new SqlParameter("@设备编号", SqlDbType.NVarChar,20),
					new SqlParameter("@报警信息编号", SqlDbType.Int,4),
					new SqlParameter("@出警方式", SqlDbType.NVarChar,50),
					new SqlParameter("@技工姓名", SqlDbType.NVarChar,50),
					new SqlParameter("@技工联系电话", SqlDbType.NVarChar,50),
					new SqlParameter("@出勤时间", SqlDbType.DateTime),
					new SqlParameter("@处理方式", SqlDbType.NVarChar,50),
					new SqlParameter("@处理结果", SqlDbType.NVarChar,20),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.服务单号;
            parameters[1].Value = model.经销商ID;
            parameters[2].Value = model.会员卡号;
            parameters[3].Value = model.设备编号;
            parameters[4].Value = model.报警信息编号;
            parameters[5].Value = model.出警方式;
            parameters[6].Value = model.技工姓名;
            parameters[7].Value = model.技工联系电话;
            parameters[8].Value = model.出勤时间;
            parameters[9].Value = model.处理方式;
            parameters[10].Value = model.处理结果;
            parameters[11].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ServiceOrder ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ServiceOrder ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public QunYing.Model.ServiceOrder GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,服务单号,经销商ID,会员卡号,设备编号,报警信息编号,出警方式,技工姓名,技工联系电话,出勤时间,处理方式,处理结果 from ServiceOrder ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
};
            parameters[0].Value = ID;

            QunYing.Model.ServiceOrder model = new QunYing.Model.ServiceOrder();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["服务单号"] != null && ds.Tables[0].Rows[0]["服务单号"].ToString() != "")
                {
                    model.服务单号 = ds.Tables[0].Rows[0]["服务单号"].ToString();
                }
                if (ds.Tables[0].Rows[0]["经销商ID"] != null && ds.Tables[0].Rows[0]["经销商ID"].ToString() != "")
                {
                    model.经销商ID = ds.Tables[0].Rows[0]["经销商ID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["会员卡号"] != null && ds.Tables[0].Rows[0]["会员卡号"].ToString() != "")
                {
                    model.会员卡号 = ds.Tables[0].Rows[0]["会员卡号"].ToString();
                }
                if (ds.Tables[0].Rows[0]["设备编号"] != null && ds.Tables[0].Rows[0]["设备编号"].ToString() != "")
                {
                    model.设备编号 = ds.Tables[0].Rows[0]["设备编号"].ToString();
                }
                if (ds.Tables[0].Rows[0]["报警信息编号"] != null && ds.Tables[0].Rows[0]["报警信息编号"].ToString() != "")
                {
                    model.报警信息编号 = int.Parse(ds.Tables[0].Rows[0]["报警信息编号"].ToString());
                }
                if (ds.Tables[0].Rows[0]["出警方式"] != null && ds.Tables[0].Rows[0]["出警方式"].ToString() != "")
                {
                    model.出警方式 = ds.Tables[0].Rows[0]["出警方式"].ToString();
                }
                if (ds.Tables[0].Rows[0]["技工姓名"] != null && ds.Tables[0].Rows[0]["技工姓名"].ToString() != "")
                {
                    model.技工姓名 = ds.Tables[0].Rows[0]["技工姓名"].ToString();
                }
                if (ds.Tables[0].Rows[0]["技工联系电话"] != null && ds.Tables[0].Rows[0]["技工联系电话"].ToString() != "")
                {
                    model.技工联系电话 = ds.Tables[0].Rows[0]["技工联系电话"].ToString();
                }
                if (ds.Tables[0].Rows[0]["出勤时间"] != null && ds.Tables[0].Rows[0]["出勤时间"].ToString() != "")
                {
                    model.出勤时间 = DateTime.Parse(ds.Tables[0].Rows[0]["出勤时间"].ToString());
                }
                if (ds.Tables[0].Rows[0]["处理方式"] != null && ds.Tables[0].Rows[0]["处理方式"].ToString() != "")
                {
                    model.处理方式 = ds.Tables[0].Rows[0]["处理方式"].ToString();
                }
                if (ds.Tables[0].Rows[0]["处理结果"] != null && ds.Tables[0].Rows[0]["处理结果"].ToString() != "")
                {
                    model.处理结果 = ds.Tables[0].Rows[0]["处理结果"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,服务单号,经销商ID,会员卡号,设备编号,报警信息编号,出警方式,技工姓名,技工联系电话,出勤时间,处理方式,处理结果 ");
            strSql.Append(" FROM ServiceOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,服务单号,经销商ID,会员卡号,设备编号,报警信息编号,出警方式,技工姓名,技工联系电话,出勤时间,处理方式,处理结果 ");
            strSql.Append(" FROM ServiceOrder ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "ServiceOrder";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}